package com.zjdiepu.www.biz.icbc;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.icbc.YwOpenCardHzicbcLogMapper;
import com.zjdiepu.www.ibiz.icbc.IYwOpenCardHzicbcLogService;
import com.zjdiepu.www.model.icbc.dto.YwOpenCardHzicbcLog;
import com.zjdiepu.www.model.icbc.ext.YwOpenCardHzicbcLogExt;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2018年03月20日 09时38分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwOpenCardHzicbcLogServiceImpl extends BaseService implements IYwOpenCardHzicbcLogService {
	public static final Logger logger = LoggerFactory.getLogger(YwOpenCardHzicbcLogServiceImpl.class);

	@Autowired
	private YwOpenCardHzicbcLogMapper ywOpenCardHzicbcLogMapper;
	
	@Override
	public void saveUpdateAndDelete(YwOpenCardHzicbcLog ywOpenCardHzicbcLog, OperateType operType) {
		switch (operType) {
		case add:
			ywOpenCardHzicbcLog.initInsertData(ywOpenCardHzicbcLog.getModifier(), ywOpenCardHzicbcLog.getModifierName());
			ywOpenCardHzicbcLogMapper.insert(ywOpenCardHzicbcLog);
			break;
		case edit:
			ywOpenCardHzicbcLog.initUpdateData(ywOpenCardHzicbcLog.getModifier(), ywOpenCardHzicbcLog.getModifierName());
			// 影响的记录数
			int updateCount = ywOpenCardHzicbcLogMapper.updateByPrimaryKeySelective(ywOpenCardHzicbcLog);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywOpenCardHzicbcLog.initDeleteData(ywOpenCardHzicbcLog.getModifier(), ywOpenCardHzicbcLog.getModifierName());
			// 影响的记录数
			int deleteCount = ywOpenCardHzicbcLogMapper.updateByPrimaryKeySelective(ywOpenCardHzicbcLog);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwOpenCardHzicbcLog> selectByExt(YwOpenCardHzicbcLogExt ywOpenCardHzicbcLogExt) {
		return ywOpenCardHzicbcLogMapper.selectByExt(ywOpenCardHzicbcLogExt);
	}
	
	@Override
	public int countByExt(YwOpenCardHzicbcLogExt ywOpenCardHzicbcLogExt){
		return ywOpenCardHzicbcLogMapper.countByExt(ywOpenCardHzicbcLogExt);
	}
	
	@Override
	public YwOpenCardHzicbcLog selectById(String id) {
		return ywOpenCardHzicbcLogMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywOpenCardHzicbcLogMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwOpenCardHzicbcLog ywOpenCardHzicbcLog) {
		return ywOpenCardHzicbcLogMapper.countRefined(ywOpenCardHzicbcLog);
	}

	@Override
	public YwOpenCardHzicbcLog selectRefined(YwOpenCardHzicbcLog ywOpenCardHzicbcLog) {
		return ywOpenCardHzicbcLogMapper.selectRefined(ywOpenCardHzicbcLog);
	}
	
	@Override
	public int deletedRefined(YwOpenCardHzicbcLog ywOpenCardHzicbcLog) {
		return ywOpenCardHzicbcLogMapper.deletedRefined(ywOpenCardHzicbcLog);
	}

	@Override
	public boolean repeat(YwOpenCardHzicbcLog ywOpenCardHzicbcLog) {
		int count = countRefined(ywOpenCardHzicbcLog);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywOpenCardHzicbcLog.getId())) {
			return true;
		}
		YwOpenCardHzicbcLog dbItem = selectRefined(ywOpenCardHzicbcLog);
		if(null == dbItem) {
			return false;
		}
		return !ywOpenCardHzicbcLog.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywOpenCardHzicbcLogMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwOpenCardHzicbcLog> ywOpenCardHzicbcLogs) {
		return ywOpenCardHzicbcLogMapper.insertBatch(ywOpenCardHzicbcLogs);
	}
}
